<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-table-cell {
            text-align: center;
            height: auto;
            white-space: normal;
        }

        .layui-upload-img {
            width: 80px;
            height: 80px;
            margin: 5px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">订单号</label>
                            <div class="layui-input-inline">
                                <input id="input1" type="text" name="orderCode" autocomplete="off" class="layui-input"
                                       value="">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">订单状态</label>
                            <div class="layui-input-inline">
                                <select id="select1" name="status" lay-filter="select1">
                                    <option value="待支付">待支付</option>
                                    <option value="待发货">待发货</option>
                                    <option value="待收货">已发货</option>
                                    <option value="" selected>全部</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="searchBtn" type="button" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="up">发货</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">详情</a>

        </script>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js"></script>
<script th:inline="none" type="text/javascript">
    layui.use(["table", "layer", "laydate", "form", "upload", "common", "element"], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var form = layui.form;
        var upload = layui.upload;
        var element = layui.element;

        $("#searchBtn").on("click", function () {
            table.render({
                elem: '#currentTableId',
                url: '/orders/list',
                toolbar: '#toolbarDemo',
                where: {keyword: $("#input1").val(), status: encodeURIComponent($("#select1").val())},
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'id', title: '订单ID', sort: true},
                    {field: 'orderCode', title: '订单号'},
                    {field: 'totalPrice', title: '订单价格'},
                    {field: 'orderDate', title: '下单时间', sort: true},
                    {field: 'status', title: '订单状态', sort: true},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            });
        });
        $("#searchBtn").click();


        $("body").on("click", "#saveBtn", function () {
            $.post("/product/save", form.val("categoryForm"), function () {
                layer.closeAll();
                $("#searchBtn").click();
            });
        });
        $("body").on("click", "#saveBtn01", function () {
            $("#searchBtn").click();
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
             if (obj.event === 'up') {
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids = [];
                var s=false;
                $.each(data, function (i, v) {
                    if (v.status!="待发货") {
                        s=true;
                    }
                    ids[i] = v.id;
                });
                console.log(ids);
                if (s) {
                    layer.msg("请只选择待发货的订单！")
                    return false;
                }
                $.post("/orders/updateStatus", {ids: ids}, function () {
                    layer.msg("发货成功！")
                    $("#searchBtn").click();
                });
            }
        });
        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $.get("/product/" + data.id, function (m) {
                    layer.open({
                        type: 1,
                        area: ['700px', '500px'],
                        title: "编辑商品",
                        content: $("#productTemplate").html()
                    });
        });
        };
        });


        form.on("select(select1)", function () {
            $("#searchBtn").click();
        });

    });
</script>
</body>
</html>